#include "JCBaseConvertor.h"

#include "JCCParserTool.h"
#include "JCCGetMemHeadAddrTool.h"

#include <iostream>

#define _parser ((TOOL::CJCCParserTool*)_parser_tool)
#define _convertor ((TOOL::CJCCGetMemHeadAddrTool*)_convertor_tool)

using namespace JCC;

JCC::CJCBaseConvertor::CJCBaseConvertor( void )
{
	_parser_tool = new TOOL::CJCCParserTool;

	_convertor_tool = new TOOL::CJCCGetMemHeadAddrTool;
}

JCC::CJCBaseConvertor::CJCBaseConvertor( const CJCBaseConvertor& val )
{
	_parser_tool = new TOOL::CJCCParserTool;

	_convertor_tool = new TOOL::CJCCGetMemHeadAddrTool;

	*this = val;
}
JCC::CJCBaseConvertor::~CJCBaseConvertor( void )
{
	delete _parser;

	delete _convertor;
}

bool JCC::CJCBaseConvertor::ToObject( std::string json_ )
{
	JCCEnd();

	if(!_parser->Classinfo()->MembersInfo()->size())
	{
		if(!_parser->StartParser(_hpp_file))
		{
			return false;
		}
	}

	if(!_convertor->AssignMemsByJson(_jcc_start_addr, _parser->Classinfo(), json_))
	{
		return false;
	}

	return true;
}

std::string JCC::CJCBaseConvertor::ToJson()
{
	std::string _ret;

	JCCEnd();

	if(!_parser->Classinfo()->MembersInfo()->size())
	{
		if(!_parser->StartParser(_hpp_file))
		{
			std::cout<<"StartParser Failed!"<<std::endl;

			return _ret;
		}
	}

	_ret = _convertor->GainJsonByMems(_jcc_start_addr, _parser->Classinfo());

	return _ret;
}

CJCBaseConvertor& JCC::CJCBaseConvertor::operator=( const CJCBaseConvertor& val )
{
	_jcc_start_addr = val._jcc_start_addr;

	_hpp_file = val._hpp_file;

	*(TOOL::CJCCParserTool*)_parser_tool = *(TOOL::CJCCParserTool*)(val._parser_tool);

	*(TOOL::CJCCGetMemHeadAddrTool*)_convertor_tool = *(TOOL::CJCCGetMemHeadAddrTool*)(val._convertor_tool);

	return *this;
}